package com.wechat.utils.kit;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Locale;

/**
 * Created by MrWang on 2016/5/24.
 * 获取spring容器中的组件
 * 比如获取 CommonService 需要在CommonServiceImpl上指定@Service（"commonService"）
 * 通过getBean("commonService") 得到 CommonService对象
 */
public class SpringContextKit implements ApplicationContextAware {
    private static ApplicationContext context = null;
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    /**
     *
     * @param beanName 需要在接口实现类上标注 如@Service("userService")
     * @param <T>
     * @return
     */
    public static <T> T getBean(String beanName){
        return (T) context.getBean(beanName);
    }

    public static String getMessage(String key){
        return context.getMessage(key, null, Locale.getDefault());
    }
}
